/*
 * cTeleporter.java
 *
 * Created on 5. juillet 2007, 08:29
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package pacman;

import java.util.ArrayList;
import java.util.Random;

/**
 * Represents a teleporter. Sends the pacman who accesses the teleporter to a random other teleporter.
 * @author julien
 */
public class cTeleporter implements iCase {
    private cCore Parent;
    
    /**
     * Creates a new instance of cTeleporter
     * @param Parent The core where the teleporter is to be created.
     */
    public cTeleporter(cCore Parent) {
        this.Parent=Parent;
    }

    /**
     * Tells if the case can be accessed by caller.
     * @param caller The object who wants to access the case.
     * @return True if caller can access the case, otherwise false.
     */
    public boolean isAccessible(Object caller) {
        return true;
    }

    /**
     * Does an access on the case, i.e. it teleports the tuxman on another teleporter.
     * @param pacman The pacman who wants to be teleported.
     */
    public void Access(cPacman pacman) {
        iCase[][] Plateau=Parent.getPlateau();
        ArrayList<Position> TeleporterList=new ArrayList<Position>();
        
        for(int i=0;i<Plateau.length;i++){
            for(int j=0;j<Plateau[i].length;j++){
                if(Plateau[i][j].getImageName().equals(this.getImageName()) && !Plateau[i][j].equals(this)){
                    TeleporterList.add(new Position(i,j));
                }
            }
        }
        
        Random rnd=new Random();
        
        pacman.setPosition(TeleporterList.get(rnd.nextInt(TeleporterList.size())));
    }

    /**
     * Returns the name of the image to be loaded for this case.
     * @return A string containing the name of the image to be loaded for this case.
     */
    public String getImageName() {
        return "teleporter";
    }
    
}
